package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.InfoTemplateDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.InfoTemplate;
import com.jyu.apts.entity.User;
import com.jyu.apts.service.InfoTemplateService;
import com.jyu.apts.service.UserService;

@RestController
public class InfoTemplateController {

	@Autowired
	InfoTemplateService infoTemplateService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("infoTemplate_html")
	@RequestMapping("/infoTemplate_html")
	public ModelAndView infoTemplate_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/infoTemplate.html");
		return mv;
	}
	
	@RequiresPermissions("infoTemplate_html")
	@RequestMapping("/addInfoTemplate")
	public ResultDto addInfoTemplate(String name,String[] dataHeads,String description,Integer visibility) {
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User user = userService.getOne(userDto.getId());
		InfoTemplate infoTemplate = new InfoTemplate();
		infoTemplate.setName(name);
		infoTemplate.setAuthor(user);
		infoTemplate.setDataHeads(sb.toString().substring(1));
		infoTemplate.setDescription(description);
		infoTemplate.setVisibility(visibility);
		infoTemplateService.save(infoTemplate);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("infoTemplate_html")
	@RequestMapping("/upInfoTemplate")
	public ResultDto upInfoTemplate(long id,String[] dataHeads,String name,String description,Integer visibility) {
		InfoTemplate infoTemplate = infoTemplateService.getOne(id);
		if(infoTemplate == null) {
			return new ResultDto("二维码模板不存在!",null,2);
		}
		StringBuilder sb = new StringBuilder("");
		if(dataHeads == null || dataHeads.length == 0) {
			sb.append("#");
		}
		for(String dataHead : dataHeads) {
			sb.append("#");
			sb.append(dataHead);
		}
		infoTemplate.setDescription(description);
		infoTemplate.setName(name);
		infoTemplate.setDataHeads(sb.toString().substring(1));
		infoTemplate.setVisibility(visibility);
		infoTemplateService.save(infoTemplate);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("infoTemplate_html")
	@RequestMapping("/delInfoTemplate")
	public ResultDto delInfoTemplate(String ids) {
		String[] idArray = ids.split(",");
		List<InfoTemplate> infoTemplates = new ArrayList<InfoTemplate>();
		for(String id : idArray) {
			InfoTemplate infoTemplate = new InfoTemplate();
			infoTemplate.setId(new Long(id));
			infoTemplates.add(infoTemplate);
		}
		infoTemplateService.delete(infoTemplates);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("infoTemplate_html")
	@RequestMapping("/getInfoTemplatePage")
	public TableDto<InfoTemplateDto> getInfoTemplatePage(Integer pageNumber,Integer pageSize,Long organizationId,String search) {
		Page<InfoTemplate> pageResult = infoTemplateService.getInfoTemplatePage(pageNumber, pageSize, organizationId, search);
		List<InfoTemplate> infoTemplates = pageResult.getContent();
		List<InfoTemplateDto> infoTemplateDtos = new ArrayList<InfoTemplateDto>();
		infoTemplates.forEach(infoTemplate->infoTemplateDtos.add(new InfoTemplateDto().convert(infoTemplate)));
		return new TableDto<InfoTemplateDto>(infoTemplateDtos,pageResult.getTotalElements());
	}
	
	@RequiresPermissions("infoTemplate_html")
	@RequestMapping("/getAllInfoTemplateByOrganization")
	public List<InfoTemplateDto> getAllInfoTemplateByOrganization(long organizationId) {
		List<InfoTemplateDto> infoTemplateDtos = new ArrayList<InfoTemplateDto>();
		List<InfoTemplate> infoTemplates = infoTemplateService.findAllByOrganization(organizationId);
		infoTemplates.forEach(infoTemplate->infoTemplateDtos.add(new InfoTemplateDto().convert(infoTemplate)));
		return infoTemplateDtos;
	}
	
}
